layui.define(['form', 'layer', 'jquery'], function (exports) {
    var MOD_NAME = 'selectMore',
        form = layui.form,
        layer = layui.layer,
        $ = layui.jquery,
        selectMore = function () { };

    selectMore.prototype.selectMoreConfig = {
        elem: '#id',
        url: '', // ajax请求地址
        option_value_key: 'areaId',  //  option配置中 value取值的key 例如：<option value="areaId"></option>
        option_text_key: 'name',     // option配置中 text取值的key  例如：<option value="areaId"></option>
        params_pid_key: 'parentId',  // ajax查询目标ID下子集的参数名 例如：params:{"parentId":1}
        params_pid_def: 0, // 初始化第一级数据时 默认参数值
        v: '1.0.1'
    }

    selectMore.prototype.render = function (config) {
        this.selectMoreConfig = $.extend(this.selectMoreConfig, config);
        this.init(config.elem);
        // 表单监听
        var t = this;
        form.on('select(selectMore)', function (data) {
            var dom = $(data.elem);
            t.changeArea(dom);
        });
    }

    // 构建地区option
    selectMore.prototype.buildRegionSelect = function (target, data) {
        var html = "";
        for (var k in data) {
            html += '<option value="' + data[k][this.selectMoreConfig.option_value_key] + '">' + data[k][this.selectMoreConfig.option_text_key] + '</option>';
        }
        var target_dom = typeof (target) == 'string' ? $(target) : target;
        $(target_dom).html(html);

        var def = target_dom.attr("data-def");
        if (def) {
            $(target_dom).val(def);
            this.changeArea(target_dom);
        }
        var target_son = target_dom.attr("data-target");

        if (target_son && data.length > 0) {
            var pid = data[0][this.selectMoreConfig.option_value_key];
            var p = {};
            p[this.selectMoreConfig.params_pid_key] = pid;
            this.selectRegionData(target_son, p);
        }
        form.render("select");
    }

    // 查询地区数据
    selectMore.prototype.selectRegionData = function (target, params = {}) {
        var t = this;
        $.ajax({
            url: this.selectMoreConfig.url,
            type: 'get',
            dataType: 'json',
            data: params,
            success: function (res) {
                // 这里可以根据系统业务逻辑 增加逻辑判断
                if (res.code == 200) {
                    t.buildRegionSelect(target, res.data);
                } else {
                    layer.msg("数据请求失败");
                }
            }
        });
    }

    selectMore.prototype.changeArea = function (dom) {
        var target = dom.attr("data-target");
        if (target) {
            var pid = dom.val();
            var p = {};
            p[this.selectMoreConfig.params_pid_key] = pid;
            this.selectRegionData(target, p);
        }
    }

    selectMore.prototype.init = function (target) {
        var p = {};
        p[this.selectMoreConfig.params_pid_key] = this.selectMoreConfig.params_pid_def;
        this.selectRegionData(target, p);
    }

    selectMore.prototype.set = function (target, value) {
        layui.$(target).attr("data-def", value);
        layui.$(target).val(value);
        this.changeArea(layui.$(target));
    }

    selectMore.prototype.setAll = function (data = []) {
        for (var i = 0, l = data.length; i < l; i++) {
            layui.$("select[lay-filter='selectMore']").eq(i).attr("data-def", data[i]);
            layui.$("select[lay-filter='selectMore']").eq(i).val(data[i]);
            this.changeArea(layui.$("select[lay-filter='selectMore']").eq(0));
        }
    }

    var selectMore = new selectMore();
    exports(MOD_NAME, selectMore);
});